﻿$(document).ready(function() {
    $("#tree").bind("loaded.jstree", function(event, data) {
        $("#tree").jstree("open_all");
    });

    $("#tree").bind("refresh.jstree", function(event, data) {
        $("#tree").jstree("open_all");
    });

    $(function() {
        $("#tree").jstree({
            plugins: ["themes", "html_data", "ui", "checkbox"],
            ui: { theme_name: "classic" },
            checkbox: { two_state: "true" },
            rules: {
                metadata: "mdata",
                use_inline: true
            }
        });

    });

    var checked_ids = [];
    document.getElementById('urls').value = "";
    $("#tree").jstree("get_checked", null, true).each
            (function() {
                checked_ids.push(this.id);
            });
    $.ajax({
        type: 'POST',
        url: '/Poll/ApplyQuestionReportGraphicalFilters',
        traditional: true,
        data: ({ pollID: $("#pollID").val(), entityIDs: checked_ids }),
        cache: false,
        async: true,
        success: function(html) {
            $('#reportGraphical').html(html);
        }
    });


    $("#applyFilters").live("click", function() {
        var checked_ids = [];
        document.getElementById('urls').value = "";
        $("#tree").jstree("get_checked", null, true).each
            (function() {
                checked_ids.push(this.id);
            });
        $.ajax({
            type: 'POST',
            url: '/Poll/ApplyQuestionReportGraphicalFilters',
            traditional: true,
            data: ({ pollID: $("#pollID").val(), entityIDs: checked_ids }),
            cache: false,
            async: true,
            success: function(html) {
                $('#reportGraphical').html(html);
            }
        });

        return false;
    });

    $("#generatePDF").live("click", function() {
        var checked_ids = [];
        $('#selectedEntities').val("");
        document.getElementById('urls').value = "";
        $("#tree").jstree("get_checked", null, true).each
                (function() {
                    checked_ids.push(this.id);
                    $('#selectedEntities').val($('#selectedEntities').val() + this.id + ',');
                });
        $('#selectedEntities').val($('#selectedEntities').val().substring(0, $('#selectedEntities').val().length - 1));
        $.ajax({
            type: 'POST',
            url: '/Poll/ApplyQuestionReportGraphicalFilters',
            traditional: true,
            data: ({ pollID: $("#pollID").val(), entityIDs: checked_ids }),
            cache: false,
            async: false,
            success: function(html) {
                $('#reportGraphical').html(html);
            }
        });

        return true;
    });
});

